package book.userdao.imp;

import book.factory.SQLExecutorFactory;
import book.userdao.UserDao;
import book.userentity.User;
import edu.nf.SQLExecutor;
import edu.nf.handler.BeanHandler;

import java.sql.SQLException;

public class UserDaoImpl implements UserDao{
    /**
     * @see :通过一个User对象来注册用户
     */
    public int insertUser(User user){
        int i=0;
        String sql="insert into user(user_class,user_name,user_sex,user_phone,user_password,user_address,user_head,user_date,user_integral,user_rank,user_hold01,user_hold02,user_hold03) values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
        SQLExecutor sse = SQLExecutorFactory.getPrototypeSQLExecutor();
        try {
            i=sse.executeUpdate(sql,user.getUserClass(),user.getUserName(),user.getUserSex(),user.getUserPhone(),user.getUserPassword(),user.getUserAddress(),user.getUserHead(),user.getUserDate(),user.getUserIntegral(),user.getUserRank(),user.getUserHold01(),user.getUserHold02(),user.getUserHold03());
        } catch (SQLException e) {
            throw new RuntimeException("Exception in insertUser.", e);
        }
        return i;
    }
    /**
     * @see :通过手机号查询出用户 判断是否登录成功
     */
    public User selectPhoneUser(String phone){
        User user;
        String sql="select * from user where user_phone=?";
        SQLExecutor sse = SQLExecutorFactory.getPrototypeSQLExecutor();
        BeanHandler handler=new BeanHandler(User.class);
        try {
            user = sse.executeQuery(sql,handler,phone);
        } catch (SQLException e) {
            throw new RuntimeException("Exception in selectPhoneUser.", e);
        }
        return user;
    }
    /**
     * @see :通过一个User对象来修改用户信息
     */
    public int updateUser(User user){
        int i=0;
        String sql="update user set user_class=?,user_name=?,user_sex=?,user_phone=?,user_password=?,user_address=?,user_head=?,user_date=?,user_integral=?,user_rank=?,user_hold01=?,user_hold02=?,user_hold03=? where id=?";
        SQLExecutor sse = SQLExecutorFactory.getPrototypeSQLExecutor();
        try {
            i=sse.executeUpdate(sql,user.getUserClass(),user.getUserName(),user.getUserSex(),user.getUserPhone(),user.getUserPassword(),user.getUserAddress(),user.getUserHead(),user.getUserDate(),user.getUserIntegral(),user.getUserRank(),user.getUserHold01(),user.getUserHold02(),user.getUserHold03(),user.getId());
        } catch (SQLException e) {
            throw new RuntimeException("Exception in updateUser.", e);
        }
        return i;
    }
}
